
#ifndef __PARAM_STORAGE_H
#define __PARAM_STORAGE_H

#include "stm32f1xx_hal.h"
#include "stdint.h"
#include "usr_protocol.h"

// Flash storage addresses
#define PARAM_FLASH_START_ADDR    0x0801E000  // Starting from second last page
#define PARAM_FLASH_END_ADDR      0x0801FFFF  // End of Flash

// Parameter storage version
#define PARAM_VERSION             0x01

// Return status codes
#define PARAM_OK                  HAL_OK
#define PARAM_ERROR              HAL_ERROR

// Structure for parameter area header
typedef struct {
uint32_t magic;              // Magic number to identify valid parameter area
uint32_t version;            // Parameter area version number
uint32_t crc;                // CRC32 of parameter data
uint32_t reserved;           // Reserved for future use
} ParamHeader_t;

// Public functions
HAL_StatusTypeDef Param_Load(void);           // Load parameters from Flash
HAL_StatusTypeDef Param_Save(void);           // Save parameters to Flash
HAL_StatusTypeDef Param_Reset(void);          // Reset parameters to defaults

// Auto-save related
void Param_SetModified(void);                 // Mark parameters as modified

// External declarations of parameter arrays

extern int16_t control_board_param_reg_map[2][MAX_BOARD_PARAM_REG];
extern int16_t motor_status_reg_map[2][MAX_MOTOR_REG];
void init_control_board_params(void);
HAL_StatusTypeDef Flash_Write_Data(uint32_t addr, const uint8_t* data, uint32_t length);
HAL_StatusTypeDef Flash_Erase_Pages(uint32_t start_addr, uint32_t end_addr);
uint32_t Calc_CRC32(const uint8_t* data, uint32_t length);
#endif /* __PARAM_STORAGE_H */